<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" media="all">
    <script th:src="@{/layuiadmin/layui/layui.js}"></script>

</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">消息记录
            <button class="layui-btn layui-btn-xs" id="flush">刷新</button>
        </div>
        <div class="layui-card-body" style="height: 300px;overflow-y: auto">
            <table class="layui-table" lay-size="sm">
                <thead>
                <tr>
                    <th width="100px">发送人</th>
                    <th width="800px">内容</th>
                    <th>时间</th>
                </tr>
                </thead>
                <tbody id="messageList">

                </tbody>
            </table>
        </div>
    </div>

    <div class="layui-card">
        <div class="layui-card-header">消息发送</div>
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户</label>
                    <div class="layui-input-block">
                        <select name="to">
                            <option value="">请选择</option>
                            <option th:each="m:${manages}" th:value="${m.manageName}" th:text="${m.nickName}"></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">消息</label>
                    <div class="layui-input-block">
                        <textarea name="content" placeholder="请输入内容" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <a class="layui-btn" lay-submit lay-filter="layuiadmin-app-form-submit">发送</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

</div>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$,
            form = layui.form;

        $("#messageList").html("");
        var messageList = [];
        var messageLocal = localStorage.getItem("messageList");
        if (messageLocal) {
            messageList = JSON.parse(messageLocal);
        } else {
            messageList = [];
        }
        for (var i = messageList.length - 1; i >= 0; i--) {
            var item = messageList[i];
            $("#messageList").append("<tr >\n" +
                "                    <td width=\"100px\">" + item.from + "</td>\n" +
                "                    <td width=\"80px\">" + item.content + "</td>\n" +
                "                    <td>" + item.time + "</td>\n" +
                "                </tr>")
        }

        $("#flush").click(function () {
            window.location.reload();
        })

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function (data) {
            var field = data.field; //获取提交的字段
            console.log(field)
            $.ajax({
                url: 'send',
                method: 'POST',
                data: JSON.stringify(field),
                contentType: "application/json", //必须这样写
                dataType: "json",
                beforeSend: function () {
                    layer.load(2);
                },
                success: function (res) {
                    layer.closeAll();
                    if (res.code == 0) {
                        layer.msg(res.message, {
                            icon: 1,
                            time: 1000
                        });
                    } else {
                        layer.alert(res.message, {
                            icon: 2
                        });
                    }
                },
                error: function () {
                    layer.closeAll();
                    layer.alert("系统错误", {
                        icon: 2
                    })
                }
            })
        });
    })
</script>
</body>
</html>